#include "mysocket.h"
#include "apueerror.h"

#include <unistd.h>

int Socket( int family, int type, int protocol )
{
	int	n;
	
	if ( (n = socket(family, type, protocol)) < 0 )
	{
		err_sys("socket error.orz");
	}

	return (n);
}

void Bind( int sockfd,const struct sockaddr * sa,socklen_t addrlen )
{
	if ( bind(sockfd, sa, addrlen ) < 0 )
	{
		err_quit( "Bind error.orz" );
	}
}

void Connect(int sockfd,const struct sockaddr * sa,socklen_t addrlen)
{
	if ( connect( sockfd, sa, addrlen ) < 0 )
	{
		err_quit( "Connect error.orz" );
	}
		
}

void Listen( int sockfd,int backlog )
{
	if ( listen(sockfd, backlog) < 0 )
	{
		err_quit( "Listen error.orz" );
	}
}

int Accept(int sockfd,struct sockaddr * sa,socklen_t * addrlen)
{
	int n = accept( sockfd, sa, addrlen );

	if ( n < 0 )
	{
		err_quit( "Accept error.orz" );
	}

	return n;
}


ssize_t Read( int fd, void *buf, size_t count )
{
	ssize_t n = read( fd, buf, count );
	if ( n == -1 )
	{
		err_quit( "Read error.orz" );
	}

	return n;
}

//ssize_t write(int fd, const void *buf, size_t count)
void Write(int fd,void * buf,size_t count)
{
	if ( write( fd, buf, count ) != count )
	{
		err_quit( "Write error.orz" );
	}
}

